package fr.emn.integration.synthese.controle;

import java.util.Observable;
import java.util.Observer;
import java.util.Vector;

import javax.swing.JList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import fr.emn.integration.synthese.abstraction.Cours;

public class ControlJListModules implements Observer, ListSelectionListener {
	private JList<String> liste;
	private Cours modele;

	public ControlJListModules(Cours modele, JList<String> liste) {
		this.modele = modele;
		this.liste = liste;
		Vector<String> data = this.modele.getModuleNames();
		data.add(0, "-");
		this.liste.setListData(data);
		this.liste.setSelectedIndex(0);
	}

	public void update(Observable o, Object message) {
		Integer iMessage = (Integer) message;
		if (iMessage == Cours.CHANGEMENT_MODULES) {
			Vector<String> data = this.modele.getModuleNames();
			data.add(0, "-");
			this.liste.setListData(data);
			if (this.modele.getModuleCourante() != null)
				this.liste.setSelectedValue(this.modele.getModuleCourante(), true);
			else this.liste.setSelectedIndex(0);
			this.liste.repaint();
		}
	}

	public void valueChanged(ListSelectionEvent e) {
		if (this.liste.getSelectedIndex() != -1
				&& !this.liste.getSelectedValue().equals("-"))
			this.modele.setModuleCourante(this.liste.getSelectedValue());
		else if (this.liste.getSelectedIndex() != -1)
			this.modele.setModuleCourante(null);
	}
}